﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using hcom;
using System.Data;

namespace hcms.Admin
{
	public partial class NoteList : hcom.hPage
	{
		public Model model = Global.model;
		public DataRowCollection rows;
		private hString hstr = new hString();
		//public int page;					// 页号，从1开始
		//public int pages;					// 页数
		protected void Page_Load(object sender, EventArgs e)
		{
			//page = Request["page"] == null ? 1 : int.Parse(Request["page"]);
			if (Page.IsPostBack && !IsRefresh)	// 页面提交
			{
				switch (cmd.Value)
				{
					//case "delete":
					//    delete();
					//    break;

					case "page":
					case "search":
						idsubmitstate.InnerText = "";
						break;

					case "add":
						//System.Threading.Thread.Sleep(3000);
						add();
						hidpage.Value = "1";
						idsearch.Value = "";
						idsubmitstate.InnerText = "添加完毕!";
						break;

					case "update":
						//System.Threading.Thread.Sleep(3000);
						update();
						idsubmitstate.InnerText = "修改完毕!";
						break;

					default:
						throw (new Exception("不可识别的提交命令: " + cmd.Value));
				}
			}
			//else
			//{
			//    idsubmitstate.InnerText = "新建";
			//}
				
			cmd.Value = "";
			idtitle.Value = "";
			idcontent.Value = "";
			hidnoteid.Value = "";

			int pagerows = 30;		// 每页行数
			int page = ((hidpage.Value == null) || (hidpage.Value == "")) ? 1 : int.Parse(hidpage.Value);
			int pages = 0;
			rows = getNotes(ref page, pagerows, ref pages, idsearch.Value);
			hidpage.Value = page.ToString();
			hidpages.Value = pages.ToString();
		}

		/// <summary>取数据</summary>
		/// <param name="page">【输入输出】页号，从1开始, 返回正确页号</param>
		/// <param name="pagerows">每页行数</param>
		/// <param name="pages">【输出】，返回页数</param>
		/// <returns>行集合</returns>
		public DataRowCollection getNotes(ref int page, int pagerows, ref int pages, string filter = "")
		{
			// 筛选
			string fit = ((filter == null) || (filter.Trim() == ""))
				? ""
				: string.Format(" AND (" +
					"(convert(varchar(50),noteDate,120) LIKE '%{0}%'))" +
					" OR (noteCategory LIKE '%{0}%')" +
					" OR (noteTitle LIKE '%{0}%')" +
					" OR (noteContent LIKE '%{0}%')" +
					" OR (noteIP LIKE '%{0}%')"
					, filter.Trim().Replace("'", "''"));	// 允许输入单引号

			string strsql = string.Format("SELECT Count(*) FROM pri_note " +
				"WHERE (noteDeleted = 0){0}",
				fit	// 加条件
			);
			DataTable dt = model.da.GetTable(strsql);
			pages = ((int)dt.Rows[0][0] + pagerows - 1) / pagerows;

			if (page > pages) page = pages;
			else if (page <= 0) page = 1;

			strsql = string.Format(
				"SELECT * FROM " +
					"(SELECT a.*, b.userName, ROW_NUMBER() OVER (ORDER BY noteDate DESC) as n " +
					"FROM pri_note as a LEFT JOIN hcms_Users as b ON a.noteUserID = b.userID " +
					"WHERE (noteDeleted = 0){0}) as c " +
				"WHERE n > {1} AND n <= {2}",	// 分页
				fit,	// 加条件
				(page - 1) * pagerows,
				page * pagerows
			);
			dt = model.da.GetTable(strsql);
			return dt.Rows;
		}


		/*private void delete()
		{
			// 删除选择的文档，sel.Value格式“aid1, aid2, aid3,”， ** 同时删除生成的静态文档。
			model.delArticles(sel.Value, cid);
			//Response.Write("<script>alert('删除完毕。');</script>"); 用这个样式就没装进来。
			ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "alert('删除完毕。')", true);
		}*/


		private void add()
		{
			string strsql = string.Format("insert into pri_note( noteTitle, noteContent, " +
				"noteIP, noteUserID) " +
				"values('{0}', '{1}', '{2}', '{3}');",
				hstr.TrimToSQL(idtitle.Value),
				hstr.TrimToSQL(idcontent.Value),
				//hstr.TrimToSQL(idcategory.Value),
				Request.ServerVariables["REMOTE_ADDR"],
				model.getNoteString("UserID")	// 保存用户ID
				);
			model.da.Execute(strsql);
		}

		private void update()
		{
			string strsql = string.Format("UPDATE pri_note SET noteTitle='{0}', noteContent='{1}' " +
				"WHERE noteID='{2}'",
				hstr.TrimToSQL(idtitle.Value),
				hstr.TrimToSQL(idcontent.Value),
				hidnoteid.Value
				);
			model.da.Execute(strsql);
		}




	}
}